import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2025-07-06
 * Time: 15:27
 */
public class Test32 {
    static class Node {
        int val;
        Node next;
        Node random;

        Node(int val) {
            this.val = val;
            next = null;
            random = null;
        }
    }

    public static Node randomCopy(Node head) {
        Map<Node,Node> hash = new HashMap<>();
        Node cur = head;

        while(cur != null) {
            Node node = new Node(cur.val);
            hash.put(cur,node);
            cur = cur.next;
        }

        cur = head;

        while(cur != null) {
            hash.get(cur).next = hash.get(cur.next);
            hash.get(cur).random = hash.get(cur.random);
            cur = cur.next;
        }

        return hash.get(head);
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Node[] nodes = new Node[n];
        Node head = null;

        // 1. 创建节点，存入数组
        for (int i = 0; i < n; i++) {
            int val = sc.nextInt();
            nodes[i] = new Node(val);
            if (head == null) {
                head = nodes[i];
            } else {
                Node cur = head;
                while (cur.next != null) {
                    cur = cur.next;
                }
                cur.next = nodes[i];
            }
        }

        // 2. 设置 random 指针
        for (int i = 0; i < n; i++) {
            int randomIndex = sc.nextInt();
            if (randomIndex != -1) {
                nodes[i].random = nodes[randomIndex];
            }
        }

        // 3. 复制链表
        Node copiedHead = randomCopy(head);

        // 4. 输出复制后的链表（简化格式：直接输出值）
        Node cur = copiedHead;
        while (cur != null) {
            System.out.print("[" + cur.val + ",");
            if (cur.random != null) {
                System.out.print(cur.random.val);
            } else {
                System.out.print("null");
            }
            System.out.print("]");
            if (cur.next != null) {
                System.out.print(",");
            }
            cur = cur.next;
        }
        System.out.println(); // 结束输出
    }

}
